package com.youhome.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youhome.common.exception.CustomException;
import com.youhome.service.mapper.BSendRecordSMSGatherMapper;
import com.youhome.service.model.BSendRecordSMSGather;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BSendRecordSMSGatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by chen365 on 2017/12/29.
 */
@Service
public class BSendRecordSMSGatherServiceImpl implements BSendRecordSMSGatherService {
    @Autowired
    private BSendRecordSMSGatherMapper bSendRecordSMSGatherMapper;

    @Override
    public int create(BSendRecordSMSGather record) {
        return bSendRecordSMSGatherMapper.insertSelective(record);
    }

    @Override
    public BSendRecordSMSGather selectByPrimaryKey(Long id) {
        return bSendRecordSMSGatherMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<BSendRecordSMSGather> selectByBean(Integer pageIndex, Integer pageSize, BSendRecordSMSGather record) {
        if (StringUtils.isEmpty(pageIndex) || pageIndex < 1) {
            pageIndex = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 1) {
            pageSize = 10;
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<BSendRecordSMSGather> list = bSendRecordSMSGatherMapper.selectByBean(record);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<BSendRecordSMSGather> selectByBean(Principal principal,Integer pageNumber, Integer pageSize) throws CustomException {
        Long userId = principal.getUser().getUserId();
        BSendRecordSMSGather record = new BSendRecordSMSGather();
        record.setUserId(userId);
        if (StringUtils.isEmpty(pageNumber) || pageNumber < 1) {
            pageNumber = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 1) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);
       return new PageInfo<>(bSendRecordSMSGatherMapper.selectByBean(record));
    }

}
